package org.elanfox.intellicommunity.biz.entity.equipment;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import org.elanfox.intellicommunity.biz.entity.Base2PO;
import org.elanfox.intellicommunity.biz.entity.resident.UserPO;
import org.elanfox.intellicommunity.common.constant.DataConstant;
import org.elanfox.intellicommunity.common.ienum.CardEnum;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import javax.persistence.*;

/**
 * 有源卡PO
 *
 * @author loutianxi
 * @date 2019-12-04
 */
@Entity
@Data
@Table(name = "t_card")
public class CardPO extends Base2PO {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "card_id")
    private Integer cardId;
    /**
     * 卡号
     */
    @Column(name = "card_no")
    private Long cardNo;
    /**
     * 卡类型
     */
    @Column(name = "type")
    private CardEnum.TypeEnum type;
    /**
     * 卡版本
     */
    @Column(name = "version")
    private String version;
    /**
     * 卡报警状态
     */
    @Column(name = "status")
    private boolean status;
    /**
     * 冻结状态
     */
    @Column(name = "enable")
    private boolean enable;
    /**
     * 电压状态
     */
    @Column(name = "voltage_status")
    private CardEnum.VoltageStatusEnum voltageStatus;

    /**
     * 删除状态(1.已注销)
     */
    @Column(name = "delete_status")
    private boolean deleteStatus;

    /**
     * 关联用户
     */
    @OneToOne
    @JoinColumn(name = "rel_user_id")
    @JsonIgnoreProperties("card")
    @NotFound(action = NotFoundAction.IGNORE)
    private UserPO user;

    public String getEnableStyle() {
        return enable ? DataConstant.GREEN_STYLE : DataConstant.RED_STYLE;
    }

    public String getStatusStyle() {
        return status ? DataConstant.RED_STYLE : DataConstant.GREEN_STYLE;
    }


    public String getDeleteStyle() {
        return deleteStatus ? DataConstant.RED_STYLE : DataConstant.GREEN_STYLE;
    }


    public String getVoltageStatusStyle() {
        if (voltageStatus == null) {
            return "";
        }
        if (voltageStatus == CardEnum.VoltageStatusEnum.STATUS_0) {
            return DataConstant.GREEN_STYLE;
        }
        return DataConstant.RED_STYLE;
    }

    public boolean cardEnabled() {
        return this.enable && !this.deleteStatus;
    }

    /**
     * 初始化卡状态
     */
    public void init() {
        this.status = false;
        this.enable = true;
        this.voltageStatus = CardEnum.VoltageStatusEnum.STATUS_0;
        this.deleteStatus = false;
    }
}
